﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using YXShop.IDAL.Shop;
using System.Collections.Generic;

namespace YXShop.SQLServerDAL.Shop
{
    public class ShopCategory:IShopCategory
    {
        #region  "DataBase Operation"
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(YXShop.Model.Shop.ShopCategory model)
        {
            SqlParameter[] paras = (SqlParameter[])this.ValueParas(model);
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into yxs_shopcategory(");
            strSql.Append("name,pid,path,sort,isuse,adddate)");
            strSql.Append(" values (");
            strSql.Append("@name,@pid,@path,@sort,@isuse,@adddate)");
            strSql.Append(";select @@IDENTITY");
            object obj = ChangeHope.DataBase.SQLServerHelper.GetSingle(strSql.ToString(), paras);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }


        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Amend(YXShop.Model.Shop.ShopCategory model)
        {
            SqlParameter[] paras = (SqlParameter[])this.ValueParas(model);
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update yxs_shopcategory set ");
            strSql.Append("name=@name,");
            strSql.Append("pid=@pid,");
            strSql.Append("path=@path,");
            strSql.Append("sort=@sort,");
            strSql.Append("isuse=@isuse,");
            strSql.Append("adddate=@adddate");
            strSql.Append(" where id=@id ");
            ChangeHope.DataBase.SQLServerHelper.ExecuteSql(strSql.ToString(), paras);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(string id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from yxs_shopcategory ");
            strSql.Append(" where [id] in (" + id + ") ");
            ChangeHope.DataBase.SQLServerHelper.ExecuteSql(strSql.ToString());
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Amend(int id, string columnName, object value)
        {
            string sequel = "Update [yxs_shopcategory] set ";
            sequel = sequel + "[" + columnName + "] =@Value ";
            sequel = sequel + UpdateWhereSequel;
            SqlParameter[] paras = new SqlParameter[] {
                new SqlParameter("@Value", value),
                new SqlParameter("@id", id) 
            };
            object obj = ChangeHope.DataBase.SQLServerHelper.GetSingle(sequel, paras);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        #endregion

        #region  "Data Load"
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public YXShop.Model.Shop.ShopCategory GetModelByID(int id)
        {
            YXShop.Model.Shop.ShopCategory model = new YXShop.Model.Shop.ShopCategory();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,name,pid,path,sort,isuse,adddate from yxs_shopcategory ");
            strSql.Append(" where [id]=@id ");
            SqlParameter[] parameters = (SqlParameter[])this.ValueIDPara(id);
            using (SqlDataReader reader = ChangeHope.DataBase.SQLServerHelper.ExecuteReader(strSql.ToString(), parameters))
            {
                if (reader.Read())
                {
                    model.ID = reader.GetInt32(0);
                    model.Name = reader.GetString(1);
                    model.Pid = reader.GetInt32(2);
                    model.Path = reader.GetString(3);
                    model.Sort = reader.GetInt32(4);
                    model.IsUse = reader.GetInt32(5);
                    model.AddDate = reader.GetDateTime(6);
                }
            }
            return model;
        }

        /// <summary>
        /// 所有数据集合
        /// </summary>
        /// <returns></returns>
        public List<YXShop.Model.Shop.ShopCategory> GetAll()
        {
            List<YXShop.Model.Shop.ShopCategory> list = new List<YXShop.Model.Shop.ShopCategory>();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,name,pid,path,sort,isuse,adddate from yxs_shopcategory ");
            strSql.Append(" where 1=1");
            strSql.Append(" order by sort asc");
            using (SqlDataReader reader = ChangeHope.DataBase.SQLServerHelper.ExecuteReader(strSql.ToString()))
            {
                while (reader.Read())
                {
                    YXShop.Model.Shop.ShopCategory model = new YXShop.Model.Shop.ShopCategory();
                    model.ID = reader.GetInt32(0);
                    model.Name = reader.GetString(1);
                    model.Pid = reader.GetInt32(2);
                    model.Path = reader.GetString(3);
                    model.Sort = reader.GetInt32(4);
                    model.IsUse = reader.GetInt32(5);
                    model.AddDate = reader.GetDateTime(6);
                    list.Add(model);
                }
            }
            return list;
        }

        /// <summary>
        /// 得到指定条件的所有短消息
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        public List<YXShop.Model.Shop.ShopCategory> GetAll(string strWhere)
        {
            List<YXShop.Model.Shop.ShopCategory> list = new List<YXShop.Model.Shop.ShopCategory>();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,name,pid,path,sort,isuse,adddate from yxs_shopcategory ");
            if (strWhere != null && strWhere != "")
            {
                strSql.Append("where " + strWhere + " ");
            }
            strSql.Append(" order by sort asc");
            using (SqlDataReader reader = ChangeHope.DataBase.SQLServerHelper.ExecuteReader(strSql.ToString()))
            {
                while (reader.Read())
                {
                    YXShop.Model.Shop.ShopCategory model = new YXShop.Model.Shop.ShopCategory();
                    model.ID = reader.GetInt32(0);
                    model.Name = reader.GetString(1);
                    model.Pid = reader.GetInt32(2);
                    model.Path = reader.GetString(3);
                    model.Sort = reader.GetInt32(4);
                    model.IsUse = reader.GetInt32(5);
                    model.AddDate = reader.GetDateTime(6);
                    list.Add(model);
                }
            }
            return list;
        }

        /// <summary>
        /// 得到不同条件得到列表
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public ChangeHope.DataBase.DataByPage GetList(string strWhere)
        {
            ChangeHope.DataBase.DataByPage dataPage = new ChangeHope.DataBase.DataByPage();
            dataPage.Sql = "[select] * [from] yxs_shopcategory [where] " + strWhere + " [order by] sort asc";
            dataPage.GetRecordSetByPage();
            return dataPage;
        }
        /// <summary>
        /// 分页列表
        /// </summary>
        /// <returns></returns>
        public ChangeHope.DataBase.DataByPage GetList()
        {
            ChangeHope.DataBase.DataByPage dataPage = new ChangeHope.DataBase.DataByPage();
            dataPage.Sql = "[select] * [from] yxs_shopcategory [where] 1=1 [order by] sort asc";
            dataPage.GetRecordSetByPage();
            return dataPage;
        }
        /// <summary>
        /// 跟据条件与排序查询
        /// </summary>
        /// <param name="OrderSort">排序</param>
        /// <param name="Conditions">条件</param>
        /// <returns></returns>
        public ChangeHope.DataBase.DataByPage GetList(string OrderSort, int pagesize,  string Conditions)
        {
            ChangeHope.DataBase.DataByPage dataPage = new ChangeHope.DataBase.DataByPage();
            dataPage.Sql = "[select] * [from] yxs_shopcategory [where] 1=1 " + Conditions + " " + OrderSort + "";
            dataPage.PageSize = pagesize;
            dataPage.GetRecordSetByPage();
            return dataPage;
        }
        #endregion

        #region "Other function"

        /// <summary>
        /// 更新条件
        /// </summary>
        protected string UpdateWhereSequel
        {
            get
            {
                return " Where [id] = @id";
            }
        }

        /// <summary>
        /// 该数据访问对象的属性值装载到数据库更新参数数组
        /// </summary>
        /// <remarks></remarks>
        public IDbDataParameter[] ValueParas(YXShop.Model.Shop.ShopCategory model)
        {
            SqlParameter[] paras = new SqlParameter[7];
            paras[0] = new SqlParameter("@id", SqlDbType.Int, 4);
            paras[0].Value = model.ID;
            paras[1] = new SqlParameter("@name", SqlDbType.NVarChar, 50);
            paras[1].Value = model.Name;
            paras[2] = new SqlParameter("@pid", SqlDbType.Int, 4);
            paras[2].Value = model.Pid;
            paras[3] = new SqlParameter("@path", SqlDbType.VarChar, 100);
            paras[3].Value = model.Path;
            paras[4] = new SqlParameter("@sort", SqlDbType.Int, 4);
            paras[4].Value = model.Sort;
            paras[5] = new SqlParameter("@isuse", SqlDbType.Int, 4);
            paras[5].Value = model.IsUse;
            paras[6] = new SqlParameter("@adddate", SqlDbType.DateTime);
            paras[6].Value = model.AddDate;
            return paras;
        }

        /// <summary>
        /// 主键参数
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IDbDataParameter[] ValueIDPara(int id)
        {
            SqlParameter[] paras = new SqlParameter[1];
            paras[0] = new SqlParameter("@id", id);
            paras[0].DbType = DbType.Int32;
            return paras;
        }

        #endregion
    }
}
